//
#include <iostream>
#include <vector>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <thread>
#include <chrono>

const int PORT = 8002;
const char* SERVER_IP = "192.168.1.105"; // 服务器IP地址
typedef struct _SpotSend {
    float Time;
    float Range;
    float Azimuth;
    float Elevation;
    float Velocity;
    float Hight;
    char Backup[20] = {0};
} SpotSend;

static float gs_range = 32322.0;

int sendToRadarLab() {
    int sock = 0;
    struct sockaddr_in serv_addr;
    int num = 1; // 目标数
    int FRAME_SIZE = sizeof(int) * 3 + sizeof(float) * 3 + 10 + num * sizeof(SpotSend);
    char *frame = new char[FRAME_SIZE];
    int *pHead = reinterpret_cast<int*>(frame);
    int *pDataType = reinterpret_cast<int*>(frame + sizeof(int));
    float *pAzi = reinterpret_cast<float*>(frame + sizeof(int)*2);
    float *pEle = reinterpret_cast<float*>(frame + sizeof(int)*2 + sizeof(float));
    float *pTime = reinterpret_cast<float*>(frame + sizeof(int)*2 + sizeof(float)*2);
    int *pNum = reinterpret_cast<int*>(frame + sizeof(int)*2 + sizeof(float)*3);
    SpotSend *pSpotSend = reinterpret_cast<SpotSend*>(frame + sizeof(int) * 3 + sizeof(float) * 3 + 10);

    float send_buffer[16];
    std::cout << "startTcpClient 1" << std::endl;
    // 创建socket
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        std::cerr << "Socket creation error" << std::endl;
        return -1;
    }
    std::cout << "startTcpClient 2" << std::endl;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    // 将地址从文本转换为二进制形式
    if (inet_pton(AF_INET, SERVER_IP, &serv_addr.sin_addr) <= 0) {
        std::cerr << "Invalid address/ Address not supported" << std::endl;
        return -1;
    }
    std::cout << "startTcpClient 3" << std::endl;

    // 连接到服务器
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        std::cerr << "Connection Failed" << std::endl;
        return -1;
    }
    std::cout << "startTcpClient 4" << std::endl;
    std::size_t sendCnt = 0;
    while (true) {
        std::cout << "    startTcpClient 5" << std::endl;
        *pNum = num;
        pSpotSend->Range = gs_range;
        gs_range -= 0.1;
        std::cout << "    startTcpClient 6" << std::endl;
        // 发送数据
        sendCnt = send(sock, frame, FRAME_SIZE, 0);
        std::cout << "    startTcpClient 7 sendCnt=" << sendCnt << "; ????????????????" << std::endl;
        // 等待一段时间后发送下一组数据
        // std::this_thread::sleep_for(std::chrono::seconds(1));
        std::cout << "    startTcpClient 8" << std::endl;
    }
    std::cout << "    startTcpClient 9" << std::endl;
    // 关闭socket连接
    close(sock);
    std::cout << "    startTcpClient 10" << std::endl;
    return 0;
}

int startTcpClient() {
    int sock = 0;
    struct sockaddr_in serv_addr;
    float send_buffer[16];
    std::cout << "startTcpClient 1" << std::endl;
    // 创建socket
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        std::cerr << "Socket creation error" << std::endl;
        return -1;
    }
    std::cout << "startTcpClient 2" << std::endl;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    // 将地址从文本转换为二进制形式
    if (inet_pton(AF_INET, SERVER_IP, &serv_addr.sin_addr) <= 0) {
        std::cerr << "Invalid address/ Address not supported" << std::endl;
        return -1;
    }
    std::cout << "startTcpClient 3" << std::endl;

    // 连接到服务器
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        std::cerr << "Connection Failed" << std::endl;
        return -1;
    }
    std::cout << "startTcpClient 4" << std::endl;

    while (true) {
        std::cout << "    startTcpClient 5" << std::endl;
        // 填充发送缓冲区
        for (int i = 0; i < 16; ++i) {
            send_buffer[i] = static_cast<float>(i);
        }
        std::cout << "    startTcpClient 6" << std::endl;
        // 发送数据
        send(sock, send_buffer, sizeof(send_buffer), 0);
        std::cout << "    startTcpClient 7" << std::endl;
        // 等待一段时间后发送下一组数据
        std::this_thread::sleep_for(std::chrono::seconds(1));
        std::cout << "    startTcpClient 8" << std::endl;
    }
    std::cout << "    startTcpClient 9" << std::endl;
    // 关闭socket连接
    close(sock);
    std::cout << "    startTcpClient 10" << std::endl;
    return 0;
}